package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: VerifyServiceImpl
 * @Author: 南风
 * @Email: 1480207032@qq.com
 * @Description:
 * @Date: 2020/7/11 21:16
 */
@Service
public class VerifyServiceImpl implements IVerifyService {

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();

        String uuid = userVerifyRequest.getUuid();
        String username = userVerifyRequest.getUserName();
        // 1、根据 uuid 激活 user_verify
        //userVerifyResponse = authUserVerifyByUuid(userVerifyResponse, uuid);

        Example userVerifyExample = new Example(UserVerify.class);
        userVerifyExample.createCriteria().andEqualTo("uuid", uuid);
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(userVerifyExample);
        UserVerify userVerify = userVerifies.size() >= 1 ? userVerifies.get(0):null;
        if (null == userVerify){
            // 取不到userVerify
            userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        //userVerify.setIsExpire("Y");
        userVerify.setIsVerify("Y");
        int rows = userVerifyMapper.updateByPrimaryKey(userVerify);
        if (rows != 1){
            // 更新不成功
            userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }


        // 2、根据 username 激活 member
        //userVerifyResponse = authMemberByUsername(userVerifyResponse, username);
        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username", username);
        List<Member> members = memberMapper.selectByExample(memberExample);
        Member member = members.size() >= 1 ? members.get(0):null;
        if (null == member){
            // 取不到member
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        member.setIsVerified("Y");
        int memberRows = memberMapper.updateByPrimaryKey(member);
        if (memberRows != 1){
            // 更新失败
            userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
            return userVerifyResponse;
        }



        userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        userVerifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return userVerifyResponse;
    }

    private UserVerifyResponse authMemberByUsername(UserVerifyResponse userVerifyResponse, String username) {
        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username", username);
        List<Member> members = memberMapper.selectByExample(memberExample);
        Member member = members.size() >= 1 ? members.get(0):null;
        if (null == member){
            // 取不到member
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        member.setIsVerified("Y");
        int memberRows = memberMapper.updateByPrimaryKey(member);
        if (memberRows != 1){
            // 更新失败
            userVerifyResponse.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
            return userVerifyResponse;
        }
        return userVerifyResponse;
    }

    private UserVerifyResponse authUserVerifyByUuid(UserVerifyResponse userVerifyResponse, String uuid) {
        Example userVerifyExample = new Example(UserVerify.class);
        userVerifyExample.createCriteria().andEqualTo("uuid", uuid);
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(userVerifyExample);
        UserVerify userVerify = userVerifies.size() >= 1 ? userVerifies.get(0):null;
        if (null == userVerify){
            // 取不到userVerify
            userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        //userVerify.setIsExpire("Y");
        userVerify.setIsVerify("Y");
        int rows = userVerifyMapper.updateByPrimaryKey(userVerify);
        if (rows != 1){
            // 更新不成功
            userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        return userVerifyResponse;
    }
}
